package com.controller.library;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.*;

import com.action.library.SGetCourseAction;
import com.action.library.SGetTermAction;
import com.bean.library.Student;
import com.bean.library.Term;

public class NaviSelectServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String testType = request.getParameter("test_type");
		String whichTerm = request.getParameter("which_term");
		int grade = 0;
		switch(whichTerm){
		case "freshman 1":
			grade = 1;
			break;
		case "freshman 2":
			grade = 2;
			break;
		case "sophomore 1":
			grade = 3;
			break;
		case "sophomore 2":
			grade = 4;
			break;
		case "junior 1":
			grade = 5;
			break;
		case "junior 2":
			grade = 6;
			break;
		default:
			grade = 0;
		}
		Student student = (Student)request.getSession().getAttribute("student");
		if(student!=null){
			String academy = student.getAcademy();
			String major = student.getMajor();
			SGetTermAction sGetTermAction = new SGetTermAction();
			if(grade==0){
				grade=student.getGrade();
			}
			Term term  = sGetTermAction.getTerm(grade,academy,major);
			System.out.println("一共有:"+term.getCourseList().size()+"门课");
			request.getSession().setAttribute("term", term);
			if(testType.equals("exam")){
				/***********删除session中缓存的已选课程********/
				if(request.getSession().getAttribute("selectedCourse")!=null){
					request.getSession().removeAttribute("selectedCourse");
				}
				try {
					request.getSession().setAttribute("test_type", testType);
					request.getSession().setAttribute("which_term", whichTerm);
					System.out.println("测试类型:"+testType+"学期:"+grade);
					response.sendRedirect("/Exam1/student/page/exam.jsp?"+request.getQueryString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			else if(testType.equals("practice")){
				/***********删除session中缓存的已选课程********/
				if(request.getSession().getAttribute("selectedCourse")!=null){
					request.getSession().removeAttribute("selectedCourse");
				}
				try {
					request.getSession().setAttribute("test_type", testType);
					request.getSession().setAttribute("which_term", whichTerm);
					System.out.println("测试类型:"+testType+"学期:"+grade);
					response.sendRedirect("/Exam1/student/page/simulate.jsp?"+request.getQueryString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			else if(testType.equals("free")){
				/***********删除session中缓存的已选课程********/
				if(request.getSession().getAttribute("selectedCourse")!=null){
					request.getSession().removeAttribute("selectedCourse");
				}
				try {
					request.getSession().setAttribute("test_type", testType);
					request.getSession().setAttribute("which_term", whichTerm);
					System.out.println("测试类型:"+testType+"学期:"+grade);
					response.sendRedirect("/Exam1/student/page/freeAnswer.jsp?"+request.getQueryString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/*private void Choose(String testType,String whichTerm){
		if(testType.equals("exam")){}
		else if(testType.equals("practice")){}
		else if(testType.equals("free")){}
	}
	
	private void ChooseTerm(String whichTerm){
		if(whichTerm.equals("")){}
		else if(whichTerm.equals("freshman 1")){}
		else if(whichTerm.equals("freshman 2")){}
		else if(whichTerm.equals("sophomore 1")){}
		else if(whichTerm.equals("sophomore 2")){}
		else if(whichTerm.equals("junior 1")){}
		else if(whichTerm.equals("junior 2")){}
	}*/
}

